import wepy from 'wepy'
import { config } from '@/common/config.js'

//数据操作基类
class Base {

  constructor(){
    this.host = config.host;
  }

  //网络请求方法
  http(url='',data={},method="GET"){
    return new Promise((resolve,reject)=>{
      wepy.request({
        url: this.host + url, //开发者服务器接口地址",
        data,
        method,
        success: res => {
          if( res.statusCode == 200 ){
            resolve(res)
          }else{
            wepy.showToast({ title: res.errMsg, icon: 'none' })
          }
        },
        fail: err => {
          reject(err)
        },
      });
    })

  }

  //获取缓存
  getLocal(key=''){
   return wx.getStorageSync(key)
  }

  //设置缓存
  setLocal(key='', value){
   wx.setStorageSync(key,value)
  }

}

export { Base }